package com.yy.tech.inte.context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * technical-identification$
 *
 * @author YY
 * @version 1.0.0
 * @date $ $
 * @description: TODO
 */
public class ThreadContext implements Serializable {
    private static ThreadLocal<Map<String, Object>> resources = new ThreadLocal<Map<String, Object>>();

    private static Logger logger = LoggerFactory.getLogger(ThreadContext.class);

    private ThreadContext() {
    }

    /**
     * Bind given key and given value
     *
     * @param key
     * @param value
     */
    public static void set(String key, Object value) {
        Map<String, Object> resourceMap = resources.get();

        if (resourceMap == null) {
            resourceMap = new HashMap<>();
            resources.set(resourceMap);
        }
        resourceMap.put(key, value);
        logger.debug("Bind resource to thread boot successfully, [key={},value={}]", key, value);
    }

    /**
     * Retrieve resource for the given key
     *
     * @param key
     * @return
     */
    public static Object get(String key) {
        Map<String, Object> resourceMap = resources.get();
        if (!CollectionUtils.isEmpty(resourceMap)){
            return resourceMap.get(key);
        }
        return null;
    }
    public static String getString(String key){
        return get(key,String.class);
    }

    public static <T> T get(String key,Class<T> clz){
        return get(key,clz,null);
    }

    public static <T> T get(String key,Class<T> clz,T dv){
        Map<String, Object> resourceMap = resources.get();
        if (!CollectionUtils.isEmpty(resourceMap)){
            Object value = resourceMap.get(key);
            if (null != value){
                return (T) value;
            }
        }
        return dv;
    }

    /**
     * Unbind resource for given key from get thread
     *
     * @param key
     * @return
     */
    public static Object remove(Object key) {
        Object value;
        Map<String, Object> resourceMap = resources.get();
        if (resourceMap == null || resourceMap.size() == 0 || !resourceMap.containsKey(key)) {
            logger.debug("The resource is already unbind or not exists, key={}", key);
            return null;
        }
        value = resourceMap.remove(key);
        logger.debug("Remove resource from thread, [key={},value={}]", key, value);
        if (resourceMap.size() == 0) {
            resources.set(null);
        }
        return value;
    }

    /**
     * Check whether resource has bound
     *
     * @param key
     * @return
     */
    public static boolean isExists(String key) {
        Map<String, Object> resourceMap = resources.get();
        if (resourceMap == null || resourceMap.size() == 0 || !resourceMap.containsKey(key)) {
            return false;
        }
        return true;
    }

    /**
     * Clean all resource
     *
     */
    public static void clear() {
        resources.remove();
        logger.debug("Clear resources successfully.");
    }
}
